package com.mk.account.controller;


import com.mk.account.controller.util.security.UserInfoImpl;
import com.mk.common.aspect.log.SysLog;
import com.mk.common.util.DateUtil;
import com.mk.common.util.http.AjaxResult;
import com.mk.common.util.http.HttpStatus;
import com.mk.data.dao.BillMapper;
import com.mk.model.bill.Bill;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/a1/b1/c1/bill/")
@Slf4j
public class BillController {

    @Autowired
    BillMapper billMapper;

    /**
     * 查询单个账单
     */
    @GetMapping
    @SysLog(value = "aaa")
    public AjaxResult getAll() {
        UserInfoImpl userInfo = new UserInfoImpl();
        int userId = userInfo.getUserInfo();
        log.info("getAll by: {}。",userId);
        List<Bill> bills = billMapper.queryBillByUser(userId);
        return AjaxResult.success(HttpStatus.SUCCESS,"成功",bills);
    }

    /**
     * 添加账单
     * @param bill
     */
    @PostMapping("bill")
    @SysLog
    public AjaxResult addBill(@RequestBody @Valid Bill bill){
        UserInfoImpl userInfo = new UserInfoImpl();
        int userId = userInfo.getUserInfo();
        bill.setCreateDate(DateUtil.getDatetimeForSql());
        bill.setUserId(userId);
        int i = billMapper.saveBill(bill);
        if(i==1){
            return AjaxResult.error(HttpStatus.SUCCESS,"ok");
        }else {
            return AjaxResult.error(HttpStatus.ERROR,"Sql执行错误");
        }
    }


}
